<?php
include_once("makeImage.php");
class VideoConverter
{
    var $cid;

    var $video_id;

    /**
     * carrier_id
     *    0:all
     *    1:docomo
     *    2:kddi
     *    3:softbank
     */
    var $carrier_id;

    var $flv_file_name=null;
    var $in_flv_dir=null;
    var $out_3gp_dir=null;
    
    var $flv_file=null;
    
    var $tmp_file=null;
    
    var $out_docomo_dir=null;
    var $thumbnail_docomo_dir=null;
    var $video_docomo_dir=null;
    var $video_s_docomo_dir=null;
    var $out_au_dir=null;
    var $thumbnail_au_dir=null;
    var $video_au_dir=null;
    var $out_softbank_dir=null;
    var $thumbnail_softbank_dir=null;
    var $video_softbank_dir=null;
    //docomo
    var $split_size=null;
    //au
    var $split_size_au=null;
    //softbank
     var $softbank=null;
    
    function init($para_video_id,$para_carrier_id){
	$this->split_size=1500;
        $this->split_size_au=1500;
        $this->split_size_softbank=300;
        $this->video_id=$para_video_id;
        $this->carrier_id = $para_carrier_id;

	$this->in_flv_dir = "/var/www/vhdocs/mobile/video/$this->video_id/FLV";
	$this->out_3gp_dir ="/var/www/vhdocs/mobile/video/$this->video_id";

	$this->flv_file_name="$this->video_id.flv";

	$this->flv_file="$this->in_flv_dir/$this->flv_file_name";
	$this->tmp_file="$this->out_3gp_dir/$this->flv_file_name.tmp";

	$this->out_docomo_dir="$this->out_3gp_dir/docomo";
	$this->thumbnail_docomo_dir="$this->out_docomo_dir/images/thumbnail/";
        $this->video_docomo_dir="$this->out_docomo_dir/3gp/d";
        $this->video_s_docomo_dir="$this->out_docomo_dir/3gp/s";

	$this->out_au_dir="$this->out_3gp_dir/au";
	$this->thumbnail_au_dir="$this->out_au_dir/images/thumbnail/";
        $this->video_au_dir="$this->out_au_dir/3gp";
       
	$this->out_softbank_dir="$this->out_3gp_dir/softbank";
	$this->thumbnail_softbank_dir="$this->out_softbank_dir/images/thumbnail/";
        $this->video_softbank_dir="$this->out_softbank_dir/3gp";
    }
	
    function getDuration($videofile) {
	ob_start();
	passthru("/usr/local/bin/ffmpeg -i \"{$videofile}\" 2>&1");
	$duration = ob_get_contents();
	ob_end_clean();

	$search='/Duration: (.*?),/';
	$duration=preg_match($search, $duration, $matches, PREG_OFFSET_CAPTURE, 3);
	$duration= $matches[1][0];
	$duration_array = split(':', $duration);
	$duration = $duration_array[0] * 3600 + $duration_array[1] * 60 + $duration_array[2];
	return $duration;

    }

    function getFilesByPath($src_path,$ftype) {
        $fileArray = array();
        if ( $dh = opendir ( $src_path ) ) {
            while ( false !== ( $dat = readdir ( $dh ) ) ) {
                if ( $dat != "." && $dat != ".." ) {
                     if($ftype=="3gp") {
                        array_push($fileArray,array(str_replace("tmp","3gp","$src_path/$dat"),"$src_path/$dat"));
                     }
                     else {
                          array_push($fileArray,array(str_replace("tmp","3g2","$src_path/$dat"),"$src_path/$dat"));
                     }
               }
            }
            closedir ( $dh );
        }
        return $fileArray;
    }

    function convertAu(){
      if(!file_exists("$this->video_au_dir/$this->video_id"."_001.3g2")) {
        $out_file="$this->video_au_dir/$this->video_id.tmp";
        if(!file_exists("$this->video_au_dir/$this->video_id"."_001.tmp")) {
	    $out=shell_exec("/usr/local/bin/ffmpeg -y -i \"{$this->flv_file}\"  -vcodec mpeg4 -s 176x144 -r 14.985 -b 64k -async 1 -acodec libamr_nb -ac 1 -ar 8000 -ab 12.2k -f 3g2 \"{$this->tmp_file}\"");
	    echo $out;
	    $out=shell_exec("ruby /usr/bin/atomChange.rb \"{$this->tmp_file}\" \"{$out_file}\"");
	    echo $out;
	    unlink($this->tmp_file);
	    shell_exec("chmod 644 $out_file");
            $videoTime=$this->getDuration($out_file);
	    $sn=$videoTime/16;
	    $ext ="tmp";
            for($i=1;$i<16;$i++) {
                $ss =$sn * $i;
                $file_sub =str_pad($i,3,"0",STR_PAD_LEFT );
                $out=shell_exec("/usr/local/bin/ffmpeg -i \"{$out_file}\" -ss $ss -f image2 -vcodec mjpeg \"{$this->thumbnail_au_dir}\"`eval basename \"{$out_file}\" .$ext`_$file_sub.jpg");
            }
	    makeScreenSaver($this->video_id,2);
	    makeAnimation($this->video_id,2);

	    $size = filesize( $out_file )/1024;
	    if ($size > $this->split_size_au) {
		$out=shell_exec("/usr/local/bin/MP4Box -splits $this->split_size_au \"{$out_file}\"");
		echo $out; 
                unlink($out_file);
                $datas =  $this->getFilesByPath($this->video_au_dir,"3g2");
                foreach ($datas as $data) {
                 
                    list($destfile, $file) = $data;
 		    $out=shell_exec("ruby /usr/bin/atomChange.rb \"{$file}\" \"{$destfile}\"");
 		    unlink($file);
                }
	    } else {
		rename($out_file,"$this->video_au_dir/$this->video_id"."_001.3g2");
	    }
        }
      }
    }

    function convertDocomo(){
	$out_file="$this->video_docomo_dir/$this->video_id.3gp";
	$out_s_file="$this->video_s_docomo_dir/$this->video_id"."_s.tmp";
        if(!file_exists("$this->video_docomo_dir/$this->video_id"."_001.3gp")) {
	    $out=shell_exec("/usr/local/bin/ffmpeg -y -i \"{$this->flv_file}\"  -vcodec mpeg4 -s 176x144 -r 14.985 -b 64k -async 1 -acodec libamr_nb -ac 1 -ar 8000 -ab 12.2k -f 3gp \"{$this->tmp_file}\"");
	    echo $out;                                                    
	    
            $out=shell_exec("/usr/local/bin/MP4Box -add \"{$this->tmp_file}\" -brand mmp4:1 -new \"{$out_file}\"");
            echo $out;
	    unlink($this->tmp_file);
	    shell_exec("chmod 644 $out_file");
	    $videoTime=$this->getDuration($out_file);
	    $sn=$videoTime/16;
	    $ext ="3gp";
            for($i=1;$i<16;$i++){
                $ss =$sn*$i;
                $file_sub =str_pad($i,3,"0",STR_PAD_LEFT );
                $out=shell_exec("/usr/local/bin/ffmpeg -i \"{$out_file}\" -ss $ss -f image2 -vcodec mjpeg \"{$this->thumbnail_docomo_dir}\"`eval basename \"{$out_file}\" .$ext`_$file_sub.jpg");
            }
            echo "time:=$videoTime <br/> $out";
            makeScreenSaver($this->video_id,1);
            makeAnimation($this->video_id,1);
            $size = filesize( $out_file )/1024;
	    if($size > $this->split_size) {
			
		$out=shell_exec("/usr/local/bin/MP4Box -splits $this->split_size \"{$out_file}\"");
		unlink($out_file);
	    } else {
		rename($out_file,"$this->video_docomo_dir/$this->video_id"."_001.3gp");
            }
	}
        if(!file_exists("$this->video_s_docomo_dir/$this->video_id"."_s_001.3gp")) {
         
                if(!file_exists("$this->video_s_docomo_dir/$this->video_id"."_s_001.tmp")) {
                    $out=shell_exec("/usr/local/bin/ffmpeg -y -i \"{$this->flv_file}\" -vcodec mpeg4 -b 64k -async 1 -r 14.985 -s 176x144 -acodec libamr_nb -ar 8000 -ab 12.2k -ac 1 -flags bitexact -f 3gp \"{$this->tmp_file}\"");
                    echo $out;

                    $out=shell_exec("/usr/local/bin/MP4Box -add \"{$this->tmp_file}\" -brand mmp4:1 -new \"{$out_s_file}\"");
                    echo $out;
                    unlink($this->tmp_file);
                    shell_exec("chmod 644 $out_s_file");
                    $size = filesize( $out_s_file )/1024;
                    if($size > $this->split_size) {

                        $out=shell_exec("/usr/local/bin/MP4Box -splits $this->split_size \"{$out_s_file}\"");
                        echo $out;
                        unlink($out_s_file);
                        $datas = $this->getFilesByPath($this->video_s_docomo_dir,"3gp");
                        foreach($datas as $data) {
                            list($destfile, $file) = $data;
                            $out=shell_exec("/usr/local/bin/MP4Box -add \"{$file}\" -brand mmp4:1 -new \"{$destfile}\"");
                            echo $out;
                            unlink($file);   
                        }

                    } else {
                        rename($out_s_file,"$this->video_s_docomo_dir/$this->video_id"."_s_001.3gp");
                    }
               }
         }
    }

    function convertSoftbank(){
	$out_file="$this->video_softbank_dir/$this->video_id.3gp"; 
        echo "softbankfile=$out_file";
        if(!file_exists("$this->video_softbank_dir/$this->video_id"."_001.3gp")) {
	    $out=shell_exec("/usr/local/bin/ffmpeg -y -i \"{$this->flv_file}\" -vcodec mpeg4 -s 176x144 -r 14.985 -b 64k -async 1 -acodec libamr_nb -ac 1 -ar 8000 -ab 12.2k -flags bitexact -f 3gp \"{$this->tmp_file}\"");
	    echo $out;                                                    
	    $out=shell_exec("/usr/local/bin/MP4Box -add \"{$this->tmp_file}\" -brand mmp4:1 -new \"{$out_file}\"");
	    echo $out;
	    unlink($this->tmp_file);
	    shell_exec("chmod 644 $out_file");
	    $videoTime=$this->getDuration($out_file);
	    $sn=$videoTime/16;
	    $ext ="3gp";
            for($i=1;$i<16;$i++) {
                $file_sub =str_pad($i,3,"0",STR_PAD_LEFT);
                $ss =$sn*$i;
                $out=shell_exec("/usr/local/bin/ffmpeg -i \"{$out_file}\" -ss $ss -f image2 -vcodec mjpeg \"{$this->thumbnail_softbank_dir}\"`eval basename \"{$out_file}\" .$ext`_$file_sub.jpg");
            }
	    makeScreenSaver($this->video_id,3);
            makeAnimation($this->video_id,3);
	    $size = filesize( $out_file )/1024;
	    if($size > $this->split_size_softbank) {
			
                $out=shell_exec("/usr/local/bin/MP4Box -splits $this->split_size_softbank \"{$out_file}\"");
	         unlink($out_file);
	    } else {
		rename($out_file,"$this->video_softbank_dir/$this->video_id"."_001.3gp");
	
           }
        }
    }
	
    function doVonvert(){
	if($this->carrier_id == 0) {
	    $this->convertDocomo();
	    $this->convertAu();
            $this->convertSoftbank();	
	} 
	else if($this->carrier_id==1) {
	    $this->convertDocomo();
	    $this->convertAu();
	    $this->convertSoftbank();
	}
	else if($this->carrier_id==2) {
	    $this->convertAu();
	    $this->convertDocomo();
	    $this->convertSoftbank();
	}
	else if($this->carrier_id==3) {
	    $this->convertSoftbank();
	    $this->convertDocomo();
            $this->convertAu();
	}	
    }
}
?>
